home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 192 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: qsort help
  5. Date: Wed, 03 Jan 96 02:34:02 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <820636442snz@genesis.demon.co.uk>
  9. References: <4ccio7$7c0@charm.magnus.acs.ohio-state.edu>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4ccio7$7c0@charm.magnus.acs.ohio-state.edu>
  16.            xiaoyi@bmecg.bme.ohio-state.edu "Xiaoyi Wu" writes:
  17.  
  18. >Hi, I am having some problems with the qsort routine.
  19. >
  20. >
  21. >int (*compar)(int *a, int *b)
  22.  
  23. This declares a pointer to a function. A function pointer can't have a
  24. function body.
  25.  
  26. >{
  27. >  if (*a < *b) return -1;
  28. >  else if (*a == *b) return 0;
  29. >  else return 1;
  30. >}
  31.  
  32. The prototype for qsort is:
  33.  
  34. void qsort(void *base, size_t nmemb, size_t size,
  35.            int (*compar)(const void *, const void *));
  36.  
  37. This means that it takes a pointer to a comparison function that has the
  38. following prototype:
  39.  
  40. int compar(const void *, const void *);
  41.  
  42. The parameters of the function *must* be const void *, they can't be
  43. anything else. So your comparison function could look something like:
  44.  
  45. int compar(const void *v1, const void *v2)
  46. {
  47.     const int *p1 = v1;
  48.     const int *p2 = v2;
  49.  
  50.     if (*p1 < *p2) return -1;
  51.     else if (*p1 == *p2) return 0;
  52.     else return 1;
  53. }
  54.  
  55. void * pointers are assignment compatible with other types of pointer
  56. (except function pointers) but they are not guaranteed to have the
  57. same internal representation as other pointer types so you can't
  58. legally pass a void * pointer to a function expecting a pointer to
  59. a different type. The assignment gives the compiler a chance to perform
  60. any necessary internal format conversions. If no conversions are
  61. necessary the chances are that your compiler will optimise the assignments
  62. out completely.
  63.  
  64. >int return_moved_contour(int ix, int iy)
  65.  
  66. ...
  67.  
  68.  
  69. There are a lot of undeclared variabled in this function - please try to
  70. post code that will compile (as far as possible - obviously that may not be
  71. the case where the question is about a compilation error).
  72.  
  73. -- 
  74. -----------------------------------------
  75. Lawrence Kirby | fred@genesis.demon.co.uk
  76. Wilts, England | 70734.126@compuserve.com
  77. -----------------------------------------
  78.